import java.lang.reflect.Array;
import java.util.*;

public class TreeNodeDemo {
    public static void main(String[] args) {
        Integer[] arr = {1, 2, 3, 4, 5, null, 6};
        Double[] resultArr = new Double[args.length];
        TreeNode treeNode = buildTreeNode(arr);
        List<Double> averageOfLevels = averageOfLevels(treeNode);
        Double[] doubles = averageOfLevels.toArray(resultArr);

        System.out.println(Arrays.toString(doubles));
    }


    public static List<Double> averageOfLevels(TreeNode root) {
        List<Double> arrayList = new ArrayList<>();
        if (root == null) {
            return arrayList;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            double sum = 0;
            for (int i = 0; i < size; i++) {
                TreeNode temp = queue.remove();
                sum += temp.value;
                if (temp.left != null) {
                    queue.add(temp.left);
                }
                if (temp.right != null) {
                    queue.add(temp.right);
                }
            }
            arrayList.add(sum / size);
        }
        return arrayList;
    }


    public static class TreeNode {

        int value;

        TreeNode left;

        TreeNode right;

        public TreeNode() {
        }

        public TreeNode(int value) {
            this.value = value;
        }
    }
    private static TreeNode buildTreeNode(Integer[] arr) {
        TreeNode right_right_3 = new TreeNode(arr[6]);
        TreeNode right_3 = new TreeNode(arr[4]);
        TreeNode left_3 = new TreeNode(arr[3]);
        TreeNode right2 = new TreeNode(arr[2]);
        TreeNode left2 = new TreeNode(arr[1]);
        TreeNode root = new TreeNode(arr[0]);
        left2.left = left_3;
        left2.right = right_3;
        right2.right = right_right_3;
        root.left = left2;
        root.right = right2;
        return root;


    }
}









